home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char SccsId[]= "@(#)palette.c V1.12 3/13/95";
- #endif
- /*
- | file -- palette.c
- |====================================================================
- |
- | This program shows how to use DV-Tools to manipulate and
- | create a customized color table. When run in a X windows
- | environment, the device used should NOT specify the ':d' option
- | and any X configuration file (such as .Xdefaults) should
- | have the entry DataViews*useDefaultColormap set to NO. Examples
- | of valid device strings include "x", "x:nd" and "x:256".
- |
- | The program displays a view of a color palette along with three
- | sliders. Each slider corresponds to one of the three additive
- | primaries, red, green, and blue.
- |
- | The color palette represents the current available colors in
- | the color table. The user is allowed to change the color
- | represented at a particular color index to a new color and
- | thereby create a new color table to be used by applications.
- |
- | The user can save the color table entries in a file by selecting
- | the save button. A filename will be prompted for to save the
- | color table information to.
- |
- | Select the QUIT button or type q to exit the program.
- |
- |=====================================================================
- */
- #include <windows.h>
- /*
- * DV-Tools header files
- */
- #include "std.h" /* <stdio.h> etc., scalar & macro definitions */
- #include "dvstd.h" /* public types & constants */
- #include "dvtools.h" /* constants used by T routines */
- #include "dvGR.h" /* constants used by window mgt & GR routines */
- #include "VOstd.h" /* constants used by VO & VOob routines */
- #include "Tfundecl.h" /* T routines (screens, drawports & views) */
- #include "VOfundecl.h" /* VO routines (objects) */
- #include "VUerfundecl.h" /* VUer routines (event handling routines) */
- #include "VGfundecl.h" /* VG routines (get info from dgp & vdp) */
- #include "GRfundecl.h" /* GR routines (interface to display device) */
-
- /* Constants */
- #define DVPATH (char *)NULL
- #define DISPFORMS_STB (char *)NULL
- #define DVDEVICE (char *)NULL
- #define DVCOLORTABLE (char *)NULL
- #define VIEW_NAME "palette.v"
- #define SCREEN_VIEWPORT (RECTANGLE *)NULL
- #define DRAWING_VIEWPORT (RECTANGLE *)NULL
- #define BRIGHT 1
- #define DIM 2
- #define PRINT 3
- #define QUIT 4
-
- /* Define global variables */
- DRAWPORT drawport; /* how & where to display picture, picture frame */
- OBJECT color_palette, /* color palette input object */
- menu_bar, /* menu input object */
- msg_object, /* vector text message object */
- slider_r, /* slider for red value */
- slider_g, /* slider for green value */
- slider_b, /* slider for blue value */
- color_block, /* rectangle representing color block */
- text_input; /* text entry input object */
- int Color_Index, /* buffer for palette representing color index */
- Menu_Item, /* buffer for menu input object */
- Red_Value, /* buffer for red slider */
- Green_Value, /* buffer for green slider */
- Blue_Value, /* buffer for blue slider */
- Quit = NO; /* flag to quit program */
- char filename[40] = {0}; /* filename to store color table */
- COLOR_TABLE *color_table; /* color table */
-
- /* Functions defined in palette.c */
- int Palette_Pick V_P_((OBJECT client, EVENT_REQUEST er,
- int label, OBJECT loc, ADDRESS args));
- int Handle_RGB_Sliders V_P_((OBJECT client, EVENT_REQUEST er,
- int label, OBJECT loc, ADDRESS args));
- int Menu_Pick V_P_((OBJECT client, EVENT_REQUEST er,
- int label, OBJECT loc, ADDRESS args));
-
- void Bright V_P_((void));
- void Dim V_P_((void));
- void Set_colorblock V_P_((void));
- int SaveColorFile V_P_((OBJECT client, EVENT_REQUEST er, int label,
- OBJECT loc, ADDRESS args));
- ADDRESS RebindVdps V_P_((OBJECT data_obj, ADDRESS vdp, ADDRESS argblock));
-
- /*
- * MAIN PROGRAM
- */
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow )
- {
- int argc = 0;
- char **argv;
- /*
- * program arguments
- * argv[1] - display device name (default is to use DVDEVICE)
- */
-
- /* Define & initialize device name and view filename */
- char *device_name = DVDEVICE; /* default device name */
- char *view_name = VIEW_NAME; /* default view name */
-
- /* Define display variables */
- OBJECT screen; /* display device, the window */
- VIEW view; /* picture representation of the view file */
-
- /* Control loop variables */
- OBJECT location; /* the event representation */
- int event_status; /* the status of event requests */
-
- /* Other variables */
- OBJECT drawing; /* graphical representation of screen */
-
- /*--------------------
- * Initialization
- *
- * TInit: perform the initialization of DV-Tools
- * TInit reads your configuration file and any
- * environment variables or logical names set.
- */
- make_argv(&argc,&argv,GetCommandLine());
- TInit( DVPATH, DISPFORMS_STB );
-
- /*
- * TscOpenSet: open a device as a screen object using
- * specified attributes
- * TscErase: erase the entire screen in the default
- * background color
- *
- * Set exposure block to YES to insure the window
- * is ready for drawing when TdpDraw is called.
- */
- if (argc > 1)
- device_name = argv[1];
- screen = TscOpenSet (device_name, DVCOLORTABLE,
- V_X_EXPOSURE_BLOCK, YES,
- V_ACTIVE_CURSOR, V_END_OF_LIST);
- if (!screen)
- {
- printf ("Must specify device on command line or");
- printf (" in DataViews configuration file.\n");
- S_EXIT (EXIT_ERR);
- }
- TscErase (screen);
-
- /*
- * VOscWinEventMask: sets the screen's window event mask
- */
- VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS | V_EXPOSE | V_RESIZE,
- (ULONG) 0);
-
- /*
- * TviLoad: Load a view in from a file,
- * default view file palette.v
- * TdpCreate: Create a DV-Tools window, a drawport.
- * The drawport is attached to the screen object
- * specified while view specifies the view to be
- * displayed on the screen.
- */
- view = TviLoad (view_name);
- if (!view)
- {
- printf ("Could not load view from file ");
- printf ("%s.\n", view_name);
- S_EXIT (EXIT_ERR);
- }
- drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
-
- /*
- * TviGetDrawing: Gets a view's drawing object
- * TobForEachVdp: Traverses all variable descriptors
- * in an object
- *
- * Traverse all variable descriptors in the drawing object
- * call the function RebindVdps for each variable descriptor.
- */
- drawing = TviGetDrawing (view);
- TobForEachVdp (drawing, RebindVdps, (ADDRESS) NULL);
-
- /*
- * TdrGetNamedObject: Gets a named object from a
- * drawing.
- *
- * Get the color palette input object, each slider representing
- * red, green and blue values, the menu input object, and the
- * color block from the view's drawing.
- */
- color_palette = TdrGetNamedObject (drawing, "color_palette");
- slider_r = TdrGetNamedObject (drawing, "red_slider");
- slider_g = TdrGetNamedObject (drawing, "green_slider");
- slider_b = TdrGetNamedObject (drawing, "blue_slider");
- menu_bar = TdrGetNamedObject (drawing, "menu");
- color_block = TdrGetNamedObject (drawing, "color_block");
-
- /*
- * VOvtReference: Increments reference cnt of vector text obj
- * VOinReference: Increments reference cnt of input obj
- * VOdrObDelete: Deletes an object from a drawing.
- *
- * Obtain the message string object and the text entry input
- * object. Remove these objects from the drawing. These objects
- * will be drawn when needed.
- */
- msg_object = VOvtReference (TdrGetNamedObject (drawing, "message"));
- VOdrObDelete (drawing, msg_object);
- text_input = VOinReference (TdrGetNamedObject (drawing, "input_text"));
- VOdrObDelete (drawing, text_input);
-
- /*
- * VUerServiceResultPost: Post a service result request
- * with the event handler.
- *
- * Post a service result request which monitors the
- * input objects. The request specifies the
- * type of the service result flag to be generated.
- */
- VUerServiceResultPost ((OBJECT) 1, Palette_Pick,
- (ADDRESS) NULL, 0, color_palette, (int)INPUT_DONE, (int)0);
-
- VUerServiceResultPost ((OBJECT) 1, Handle_RGB_Sliders,
- (ADDRESS) NULL, 0, slider_r,
- (int)INPUT_DONE | INPUT_ACCEPT, (int)0);
-
- VUerServiceResultPost ((OBJECT) 2, Handle_RGB_Sliders,
- (ADDRESS) NULL, 0, slider_g,
- (int)INPUT_DONE | INPUT_ACCEPT, (int)0);
-
- VUerServiceResultPost ((OBJECT) 3, Handle_RGB_Sliders,
- (ADDRESS) NULL, 0, slider_b,
- (int)INPUT_DONE | INPUT_ACCEPT, (int)0);
-
- VUerServiceResultPost ((OBJECT) 1, Menu_Pick,
- (ADDRESS) NULL, 0, menu_bar, (int)INPUT_DONE, (int)0);
-
- VUerServiceResultPost ((OBJECT) 1, (VUERFCNFUNPTR)SaveColorFile,
- (ADDRESS) NULL, 0, text_input, (int)INPUT_DONE, (int)0);
-
- /*
- * TdpDraw: Draw the contents of the drawport
- */
- TdpDraw (drawport);
-
- /*
- * GRg_color_table: Get the color table
- */
- GRg_color_table (&color_table);
-
- /*--------------------
- * Control loop
- *
- * Poll the event queue for window events. If the key
- * represents the character 'q' or 'Q' then quit the program.
- * Events occurring within input objects will be handled
- * through the event request handler VUerHandleLocEvent
- * and continually update the dynamic objects.
- */
- FOREVER
- {
- /*
- * VOloWinEventPoll: Poll for the next window event.
- * The polling mode used is V_WAIT.
- * Therefore, VOloWinEventPoll does not
- * return until a masked event is
- * generated. V_WAIT always produces
- * a valid location object.
- */
- location = VOloWinEventPoll (V_WAIT);
-
- /*
- * TdpEraseObject: Erases an object in the drawport
- * VOvtSetString: Sets new vector text obj string value
- * TdpDrawObject: Draws an object in the drawport
- *
- * Erase the current displayed string and clear the vector
- * text string value.
- */
- TdpEraseObject (drawport, msg_object);
- VOvtSetString (msg_object, "");
- TdpDrawObject (drawport, msg_object);
-
- /*
- * VUerHandleLocEvent: Service the event. This routine will check
- * if the event is used by any input objects
- * that may be in the view.
- */
- event_status = VUerHandleLocEvent (location);
- if (event_status == INPUT_UNUSED)
- {
- /*
- * VOloType: returns the type of event. These types
- * match event types specified in VOscWinEventMask.
- */
- switch (VOloType (location))
- {
- case V_KEYPRESS:
- /*
- * Check key selected.
- * VOloKeySym: Returns the key symbol value of the
- * location object
- *
- * If the key symbol represents the characters 'q'
- * or 'Q' then quit the program.
- */
- switch (VOloKeySym (location))
- {
- case 'q':
- case 'Q':
- Quit = YES;
- break;
- default:
- break;
- }
- break;
-
- case V_EXPOSE:
- /*
- * VOloRegion: Returns a rectangle representing the
- * exposed region on the screen.
- * TscRedraw: After erasing, redraws all the drawports
- * in the screen.
- * A portion of the window has been exposed and needs
- * to be redrawn.
- */
- TscRedraw (screen, VOloRegion (location));
- break;
-
- case V_RESIZE:
- /*
- * TscReset: Resets all screen drawports after
- * window resizing
- * The window size has been changed.
- */
- TscReset (screen);
- break;
-
- default:
- break;
- }
- }
-
- /* exit the program */
- if (Quit == YES)
- break;
-
- /*
- * TdpDrawNext: Update all dynamic objects within a
- * drawport's view.
- */
- TdpDrawNext (drawport);
- }
-
- /*--------------------
- * Termination
- *
- * TscErase: Erase the entire screen in the default
- * background color
- * TdpDestroy: Destroy the drawport,
- * TviCloseData: Close the data sources of the view
- * TviDestroy: Destroy the view, freeing the allocated memory
- * TscClose: Closes the screen object
- * TTerminate: Perform the clean-up for DV-Tools
- */
- TscErase (screen);
- TdpDestroy (drawport);
- TviDestroy (view);
- TscClose (screen);
- TTerminate ();
- return (EXIT_OK);
- }
-
-
- /*---------------
- * Palette_Pick -- User selected a color entry from the palette.
- * Determine and set the sliders based on the red, green &
- * blue values from the palette.
- */
- /*ARGSUSED*/
- int
- Palette_Pick (client, er, label, loc, args)
- OBJECT client;
- EVENT_REQUEST er;
- int label;
- OBJECT loc;
- ADDRESS args;
- {
- Red_Value = color_table->ct[Color_Index].red;
- Green_Value = color_table->ct[Color_Index].green;
- Blue_Value = color_table->ct[Color_Index].blue;
- Set_colorblock ();
- return (int) INPUT_USED;
- }
-
-
- /*---------------------
- * Handle_RGB_Sliders -- Assign red, green & blue values to the
- * color table and update the color block.
- */
- /*ARGSUSED*/
- int
- Handle_RGB_Sliders (client, er, label, loc, args)
- OBJECT client;
- EVENT_REQUEST er;
- int label;
- OBJECT loc;
- ADDRESS args;
- {
- color_table->ct[Color_Index].red = Red_Value;
- color_table->ct[Color_Index].green = Green_Value;
- color_table->ct[Color_Index].blue = Blue_Value;
- Set_colorblock ();
- return (int) INPUT_USED;
- }
-
-
- /*------------
- * Menu_Pick -- Handle picks from the main menu input object.
- */
- /*ARGSUSED*/
- int
- Menu_Pick (client, er, label, loc, args)
- OBJECT client;
- EVENT_REQUEST er;
- int label;
- OBJECT loc;
- ADDRESS args;
- {
-
- /*
- * Examine the buffer which is updated by the input object with
- * the menu selection. Perform the appropriate action based on
- * the menu selection.
- */
- switch (Menu_Item)
- {
- case BRIGHT:
- Bright ();
- break;
-
- case DIM:
- Dim ();
- break;
-
- case PRINT:
- /*
- * VOinState: Set the input object activation state to INACTIVE
- * TdpDrawObject: Draw an object in the drawport
- *
- * Prompt the user for the file name in which to save the
- * color table.
- */
- VOinState (color_palette, INACTIVE);
- VOinState (slider_r, INACTIVE);
- VOinState (slider_g, INACTIVE);
- VOinState (slider_b, INACTIVE);
- VOinState (menu_bar, INACTIVE);
- TdpDrawObject (drawport, text_input);
- break;
-
- case QUIT:
- Quit = YES;
- break;
-
- default:
- break;
- }
- return (int) INPUT_USED;
- }
-
-
- /*---------
- * Bright -- Increment the slider input objects representing
- * the Red, Green and Blue value by one as well as incrementing
- * the color table entry. And lastly, update the color block.
- */
- void Bright ()
- {
-
- if (color_table->ct[Color_Index].red < 255)
- {
- Red_Value++;
- color_table->ct[Color_Index].red =
- color_table->ct[Color_Index].red + 1;
- }
-
- if (color_table->ct[Color_Index].green < 255)
- {
- Green_Value++;
- color_table->ct[Color_Index].green =
- color_table->ct[Color_Index].green + 1;
- }
-
- if (color_table->ct[Color_Index].blue < 255)
- {
- Blue_Value++;
- color_table->ct[Color_Index].blue =
- color_table->ct[Color_Index].blue + 1;
- }
-
- Set_colorblock ();
- }
-
-
- /*------
- * Dim -- Decrement the slider input objects representing the
- * Red, Green and Blue value by one as well as decrementing
- * the color table entry. And lastly, update the color block.
- */
- void Dim ()
- {
-
- if (color_table->ct[Color_Index].red > 0)
- {
- Red_Value--;
- color_table->ct[Color_Index].red =
- color_table->ct[Color_Index].red - 1;
- }
-
- if (color_table->ct[Color_Index].green > 0)
- {
- Green_Value--;
- color_table->ct[Color_Index].green =
- color_table->ct[Color_Index].green - 1;
- }
-
- if (color_table->ct[Color_Index].blue > 0)
- {
- Blue_Value--;
- color_table->ct[Color_Index].blue =
- color_table->ct[Color_Index].blue - 1;
- }
- Set_colorblock ();
- }
-
-
- /*-----------------
- * Set_colorblock -- Set the working color table and update
- * the filled rectangle representing the color to be used
- * for the currently selected color index.
- */
- void Set_colorblock ()
- {
- ATTRIBUTES Attributes;
-
- /*
- * GRs_color_table: Sets up the color table
- * VOobAtGet: Get current attributes of an object
- * VOcoCreate: Create a color object
- * VOobAtSet: Set new attributes of an object
- * TdpDrawObject: Draw an object in the drawport
- *
- * Set the color table for the device. retrieve the attributes
- * for the filled rectangle block and set the foreground color.
- * Finally, draw the object.
- */
- GRs_color_table (color_table);
- VOobAtGet (color_block, &Attributes);
- Attributes.foreground_color = VOcoCreate (COLOR_COMPONENTS,
- color_table->ct[Color_Index].red,
- color_table->ct[Color_Index].green,
- color_table->ct[Color_Index].blue);
- VOobAtSet (color_block, &Attributes);
- TdpDrawObject (drawport, color_block);
- }
-
-
- /*----------------
- * SaveColorFile -- saves the rgb values for each entry in
- * the color table to the filename specified by the user.
- */
- /*ARGSUSED*/
- int
- SaveColorFile (client, er, label, loc, args)
- OBJECT client;
- EVENT_REQUEST er;
- int label;
- OBJECT loc;
- ADDRESS args;
- {
- int i;
- FILE *fp;
-
- /*
- * TdpEraseObject: Erase an object in a drawport
- * VOinState: Set the activation flag for the input object
- *
- * Erase the popup text entry input object and reactivate the
- * other input areas of the screen.
- */
- TdpEraseObject (drawport, text_input);
- VOinState (color_palette, ACTIVE);
- VOinState (slider_r, ACTIVE);
- VOinState (slider_g, ACTIVE);
- VOinState (slider_b, ACTIVE);
- VOinState (menu_bar, ACTIVE);
-
- /*
- * Check the value of filename. If the user did not enter a
- * name then return.
- */
- if (strcmp (filename, "") == 0)
- return (int) INPUT_USED;
-
- /*
- * Open and create the file in which the color table
- * information will be stored. Print the color table
- * entries to the file and close the stream.
- */
- fp = fopen (filename, "w");
- if (fp == (FILE *) 0)
- {
- TdpEraseObject (drawport, msg_object);
- VOvtSetString (msg_object, "Error: Color table was not saved");
- TdpDrawObject (drawport, msg_object);
- return (int) INPUT_USED;
- }
-
- for (i = 0; i < color_table->ctsize; i++)
- fprintf (fp, " %d\t %d\t %d\n",
- color_table->ct[i].red,
- color_table->ct[i].green,
- color_table->ct[i].blue);
- fclose (fp);
-
- /*
- * Indicate that the color table has been saved properly using
- * the message string object.
- */
- TdpEraseObject (drawport, msg_object);
- VOvtSetString (msg_object, "Color table saved in specified file");
- TdpDrawObject (drawport, msg_object);
-
- return (int) INPUT_USED;
- }
-
-
- /*------------
- * RebindVdp -- rebinds all the necessary vdps to appropriate
- * internal program variables.
- */
- /*ARGSUSED*/
- ADDRESS
- RebindVdps (data_obj, vdp, argblock)
- OBJECT data_obj;
- ADDRESS vdp;
- ADDRESS argblock;
- {
- char *name;
-
- /*
- * VGvdvarname: Gets a pointer to the variable name
- *
- * The name of the variable descriptor is used to
- * determine which buffer to rebind to. If the
- * variable does not have a name then return.
- */
- name = VGvdvarname (vdp);
- if (!name)
- return V_CONTINUE_TRAVERSAL;
-
- if (strcmp (name, "index") == 0)
- TvdPutBuffer (vdp, (ADDRESS) & Color_Index);
- else if (strcmp (name, "menu_bar") == 0)
- TvdPutBuffer (vdp, (ADDRESS) & Menu_Item);
- else if (strcmp (name, "R") == 0)
- TvdPutBuffer (vdp, (ADDRESS) & Red_Value);
- else if (strcmp (name, "G") == 0)
- TvdPutBuffer (vdp, (ADDRESS) & Green_Value);
- else if (strcmp (name, "B") == 0)
- TvdPutBuffer (vdp, (ADDRESS) & Blue_Value);
- else if (strcmp (name, "Text") == 0)
- TvdPutBuffer (vdp, filename);
-
- return V_CONTINUE_TRAVERSAL;
- }
-